Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
orientation.h
Go to the documentation of this file.
1 // Copyright (c) 2014, 2015, 2016, NXP Semiconductors N.V.,
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of NXP Semiconductors N.V. nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL NXP SEMICONDUCTORS N.V. BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 /*! \file orientation.h
27  \brief Functions to convert between various orientation representations
28 
29  Functions to convert between various orientation representations. Also
30  includes functions for manipulating quaternions.
31 */
32 
33 #ifndef ORIENTATION_H
34 #define ORIENTATION_H
35 
36 /// quaternion structure definition
37 typedef struct Quaternion
38 {
39  float q0; ///< scalar component
40  float q1; ///< x vector component
41  float q2; ///< y vector component
42  float q3; ///< z vector component
43 } Quaternion;
44 
45 // function prototypes
46 /// Aerospace NED accelerometer 3DOF tilt function, computing rotation matrix fR
47 void f3DOFTiltNED(
48  float fR[][3], ///< computed rotation matrix (output)
49  float fGc[] ///< calibrated accelerometer input vector
50 );
51 /// Android accelerometer 3DOF tilt function computing, rotation matrix fR
52 void f3DOFTiltAndroid(
53  float fR[][3], ///< computed rotation matrix (output)
54  float fGc[] ///< calibrated accelerometer input vector
55 );
56 /// Windows 8 accelerometer 3DOF tilt function computing, rotation matrix fR
57 void f3DOFTiltWin8(
58  float fR[][3], ///< computed rotation matrix (output)
59  float fGc[] ///< calibrated accelerometer input vector
60 );
61 /// Aerospace NED magnetometer 3DOF flat eCompass function, computing rotation matrix fR
63  float fR[][3], ///< computed rotation matrix (output)
64  float fBc[] ///< calibrated magnetometer reading (input)
65 );
66 /// Android magnetometer 3DOF flat eCompass function, computing rotation matrix fR
68  float fR[][3], ///< computed rotation matrix (output)
69  float fBc[] ///< calibrated magnetometer reading (input)
70 );
71 /// Windows 8 magnetometer 3DOF flat eCompass function, computing rotation matrix fR
73  float fR[][3], ///< computed rotation matrix (output)
74  float fBc[] ///< calibrated magnetometer reading (input)
75 );
76 /// NED: basic 6DOF e-Compass function, computing rotation matrix fR and magnetic inclination angle fDelta
77 void feCompassNED(
78  float fR[][3], ///< computed rotation matrix (output)
79  float *pfDelta, ///< magnetic inclination angle (output)
80  float *pfsinDelta, ///< sin of the inclination angle
81  float *pfcosDelta, ///< cos of the inclination angle
82  float fBc[], ///< calibrated magnetometer vector (input)
83  float fGc[], ///< calibrated accelerometer input vector (input)
84  float *pfmodBc, ///< modulus of the calibrated magnetic vector
85  float *pfmodGc ///< modulus of the calibrated accelerometer vector
86 );
87 /// Android: basic 6DOF e-Compass function, computing rotation matrix fR and magnetic inclination angle fDelta
88 void feCompassAndroid(
89  float fR[][3], ///< computed rotation matrix (output)
90  float *pfDelta, ///< magnetic inclination angle (output)
91  float *pfsinDelta, ///< sin of the inclination angle
92  float *pfcosDelta, ///< cos of the inclination angle
93  float fBc[], ///< calibrated magnetometer reading (input)
94  float fGc[], ///< calibrated accelerometer input vector (input)
95  float *pfmodBc, ///< modulus of the calibrated magnetic vector
96  float *pfmodGc ///< modulus of the calibrated accelerometer vector
97 );
98 /// Win8: basic 6DOF e-Compass function, computing rotation matrix fR and magnetic inclination angle fDelta
99 void feCompassWin8(
100  float fR[][3], ///< computed rotation matrix (output)
101  float *pfDelta, ///< magnetic inclination angle (output)
102  float *pfsinDelta, ///< sin of the inclination angle
103  float *pfcosDelta, ///< cos of the inclination angle
104  float fBc[], ///< calibrated magnetometer reading (input)
105  float fGc[], ///< calibrated accelerometer input vector (input)
106  float *pfmodBc, ///< modulus of the calibrated magnetic vector
107  float *pfmodGc ///< modulus of the calibrated accelerometer vector
108 );
109 /// extract the NED angles in degrees from the NED rotation matrix
111  float R[][3], ///< rotation matrix input
112  float *pfPhiDeg, ///< output: the roll angle range -180.0 <= Phi < 180.0 deg
113  float *pfTheDeg, ///< output: the pitch angle -90.0 <= Theta <= 90.0 deg
114  float *pfPsiDeg, ///< output: the yaw (compass) angle 0.0 <= Psi < 360.0 deg
115  float *pfRhoDeg, ///< output: For NED, the compass heading Rho equals the yaw angle Psi
116  float *pfChiDeg ///< output: the tilt angle from vertical Chi (0 <= Chi <= 180 deg)
117 );
118 /// extract the Android angles in degrees from the Android rotation matrix
120  float R[][3], ///< rotation matrix input
121  float *pfPhiDeg, ///< the roll angle -90.0 <= Phi <= 90.0 deg
122  float *pfTheDeg, ///< the pitch angle -180.0 <= The < 180.0 deg
123  float *pfPsiDeg, ///< yaw angle Psi with range 0.0 <= Psi < 360.0 deg
124  float *pfRhoDeg, ///< the compass heading angle Rho equals the yaw angle Psi
125  float *pfChiDeg ///< the tilt angle from vertical Chi (0 <= Chi <= 180 deg)
126 );
127 /// extract the Windows 8 angles in degrees from the Windows 8 rotation matrix
129  float R[][3], ///< rotation matrix input
130  float *pfPhiDeg, ///< the roll angle -90.0 <= Phi <= 90.0 deg
131  float *pfTheDeg, ///< pitch angle Theta in the range -180.0 <= The < 180.0 deg
132  float *pfPsiDeg, ///< yaw angle Psi in range 0.0 <= Psi < 360.0 deg
133  float *pfRhoDeg, ///< the compass angle Rho = 360 - Psi
134  float *pfChiDeg ///< tilt angle from vertical Chi (0 <= Chi <= 180 deg)
135 );
136 /// compute the orientation quaternion from a 3x3 rotation matrix
138  float R[][3], ///< Rotation matrix (input)
139  Quaternion *pq ///< Quaternion (output)
140 );
141 /// compute the rotation matrix from an orientation quaternion
143  float R[][3], ///< Rotation matrix (output)
144  const Quaternion *pq ///< Quaternion (input)
145 );
146 /// function compute the quaternion product qB * qC
147 void qAeqBxC(
148  Quaternion *pqA,
149  const Quaternion *pqB,
150  const Quaternion *pqC
151 );
152 /// function compute the quaternion product qA = qA * qB
153 void qAeqAxB(
154  Quaternion *pqA,
155  const Quaternion *pqB
156 );
157 /// function compute the quaternion product conjg(qA) * qB
159  const Quaternion *pqA,
160  const Quaternion *pqB
161 );
162 /// function normalizes a rotation quaternion and ensures q0 is non-negative
163 void fqAeqNormqA(
164  Quaternion *pqA
165 );
166 /// set a quaternion to the unit quaternion
167 void fqAeq1(
168  Quaternion *pqA
169 );
170 /// computes normalized rotation quaternion from a rotation vector (deg)
172  Quaternion *pq, ///< quaternion (output)
173  const float rvecdeg[], ///< rotation vector in degrees
174  float fscaling ///< delta Time
175 );
176 /// computes rotation vector (deg) from rotation quaternion
178  Quaternion *pq, ///< quaternion (input)
179  float rvecdeg[] ///< rotation vector in degrees (output)
180 );
181 /// function low pass filters an orientation quaternion and computes virtual gyro rotation rate
183  Quaternion *pq,
184  Quaternion *pLPq,
185  float flpf,
186  float fdeltat,
187  float fOmega[]
188 );
189 /// function computes the rotation quaternion that rotates unit vector u onto unit vector v as v=q*.u.q
190 /// using q = 1/sqrt(2) * {sqrt(1 + u.v) - u x v / sqrt(1 + u.v)}
191 void fveqconjgquq(
192  Quaternion *pfq,
193  float fu[],
194  float fv[]
195 );
196 
197 #endif // #ifndef ORIENTATION_H
void f3DOFMagnetometerMatrixAndroid(float fR[][3], float fBc[])
Android magnetometer 3DOF flat eCompass function, computing rotation matrix fR.
Definition: orientation.c:245
void fRotationVectorDegFromQuaternion(Quaternion *pq, float rvecdeg[])
computes rotation vector (deg) from rotation quaternion
Definition: orientation.c:862
void fqAeqNormqA(Quaternion *pqA)
function normalizes a rotation quaternion and ensures q0 is non-negative
Definition: orientation.c:999
void f3DOFTiltNED(float fR[][3], float fGc[])
Aerospace NED accelerometer 3DOF tilt function, computing rotation matrix fR.
void fLPFOrientationQuaternion(Quaternion *pq, Quaternion *pLPq, float flpf, float fdeltat, float fOmega[])
function low pass filters an orientation quaternion and computes virtual gyro rotation rate ...
Definition: orientation.c:912
void feCompassNED(float fR[][3], float *pfDelta, float *pfsinDelta, float *pfcosDelta, float fBc[], float fGc[], float *pfmodBc, float *pfmodGc)
NED: basic 6DOF e-Compass function, computing rotation matrix fR and magnetic inclination angle fDelt...
Definition: orientation.c:286
float q0
scalar component
Definition: orientation.h:39
float q3
z vector component
Definition: orientation.h:42
void fQuaternionFromRotationVectorDeg(Quaternion *pq, const float rvecdeg[], float fscaling)
computes normalized rotation quaternion from a rotation vector (deg)
Definition: orientation.c:713
quaternion structure definition
Definition: orientation.h:37
struct Quaternion Quaternion
quaternion structure definition
Quaternion qconjgAxB(const Quaternion *pqA, const Quaternion *pqB)
function compute the quaternion product conjg(qA) * qB
Definition: orientation.c:986
#define R
Definition: status.c:48
void fqAeq1(Quaternion *pqA)
set a quaternion to the unit quaternion
Definition: orientation.c:1034
void f3DOFMagnetometerMatrixWin8(float fR[][3], float fBc[])
Windows 8 magnetometer 3DOF flat eCompass function, computing rotation matrix fR. ...
Definition: orientation.c:275
void qAeqAxB(Quaternion *pqA, const Quaternion *pqB)
function compute the quaternion product qA = qA * qB
Definition: orientation.c:969
void f3DOFTiltAndroid(float fR[][3], float fGc[])
Android accelerometer 3DOF tilt function computing, rotation matrix fR.
void fAndroidAnglesDegFromRotationMatrix(float R[][3], float *pfPhiDeg, float *pfTheDeg, float *pfPsiDeg, float *pfRhoDeg, float *pfChiDeg)
extract the Android angles in degrees from the Android rotation matrix
Definition: orientation.c:564
void fWin8AnglesDegFromRotationMatrix(float R[][3], float *pfPhiDeg, float *pfTheDeg, float *pfPsiDeg, float *pfRhoDeg, float *pfChiDeg)
extract the Windows 8 angles in degrees from the Windows 8 rotation matrix
Definition: orientation.c:621
void feCompassWin8(float fR[][3], float *pfDelta, float *pfsinDelta, float *pfcosDelta, float fBc[], float fGc[], float *pfmodBc, float *pfmodGc)
Win8: basic 6DOF e-Compass function, computing rotation matrix fR and magnetic inclination angle fDel...
Definition: orientation.c:433
void fNEDAnglesDegFromRotationMatrix(float R[][3], float *pfPhiDeg, float *pfTheDeg, float *pfPsiDeg, float *pfRhoDeg, float *pfChiDeg)
extract the NED angles in degrees from the NED rotation matrix
Definition: orientation.c:508
void f3DOFMagnetometerMatrixNED(float fR[][3], float fBc[])
Aerospace NED magnetometer 3DOF flat eCompass function, computing rotation matrix fR...
Definition: orientation.c:215
void f3DOFTiltWin8(float fR[][3], float fGc[])
Windows 8 accelerometer 3DOF tilt function computing, rotation matrix fR.
void fQuaternionFromRotationMatrix(float R[][3], Quaternion *pq)
compute the orientation quaternion from a 3x3 rotation matrix
Definition: orientation.c:781
float q2
y vector component
Definition: orientation.h:41
void qAeqBxC(Quaternion *pqA, const Quaternion *pqB, const Quaternion *pqC)
function compute the quaternion product qB * qC
Definition: orientation.c:958
float q1
x vector component
Definition: orientation.h:40
void fveqconjgquq(Quaternion *pfq, float fu[], float fv[])
function computes the rotation quaternion that rotates unit vector u onto unit vector v as v=q*...
Definition: orientation.c:1044
void feCompassAndroid(float fR[][3], float *pfDelta, float *pfsinDelta, float *pfcosDelta, float fBc[], float fGc[], float *pfmodBc, float *pfmodGc)
Android: basic 6DOF e-Compass function, computing rotation matrix fR and magnetic inclination angle f...
Definition: orientation.c:359
void fRotationMatrixFromQuaternion(float R[][3], const Quaternion *pq)
compute the rotation matrix from an orientation quaternion
Definition: orientation.c:822